package com.lys.emsystem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.entity.School;
import com.lys.emsystem.service.SchoolService;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.SchoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    @GetMapping
    Result getSchool(){
        return Result.buildResult(Result.Status.OK, schoolService.selectList());
    }


    @PostMapping
    Result getSchoolList(@RequestBody(required = false) SchoolVo schoolVo) {
        IPage<School> page = schoolService.selectByNameOrNo(new Page<>(schoolVo.getPageIndex(),schoolVo.getPageSize() ), schoolVo);
        schoolVo.setSchools(page.getRecords());
        schoolVo.setTotalCount(Math.toIntExact(page.getTotal()));
        return Result.buildResult(Result.Status.OK, schoolVo);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Result deleteSchool(@RequestBody School school) {
        int i = schoolService.deleteSchool(school.getId());
        if (i == 1) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addSchool(@RequestBody School school) {
        int i = schoolService.addSchool(school);
        if (i == 1) {
            return Result.buildResult(Result.Status.OK,school);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result updateSchool(@RequestBody School school) {
        int i = schoolService.updateSchool(school);
        if (i == 1) {
            return Result.buildResult(Result.Status.OK,school);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }
    @PostMapping("/delList")
    public Result deleteSchools(@RequestBody SchoolVo schoolVo) {
        int i = schoolService.deleteSchools(schoolVo.getSchoolIds());
        if (i>0) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

}
